package com.ztgf.upload.service;

import com.ztgf.common.utils.FileCheck;
import com.ztgf.upload.models.service.StoragePoolService;
import org.apache.dubbo.config.annotation.DubboService;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;

@DubboService
public class StoragePoolServiceImpl implements StoragePoolService {
    @Value("${fdfs.storage_server}")
    private String storage_server;

    @Value("${fdfs.storage_port}")
    private String storage_port;

    @Value("${fdfs.image}")
    private String storage_image;

    @Value("${fdfs.doc}")
    private String storage_doc;

    @Value("${fdfs.compress}")
    private String storage_compress;

    @Value("${fdfs.video}")
    private String storage_video;

    @Value("${fdfs.other}")
    private String storage_other;

    @Override
    public StorageClient1 getStorageClient(StorageServer storageServer){
        System.out.println("getStorageClient");
        StorageClient1 storageClient = null;
        try {
            //加载配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            //创建新的TrackerClient
            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            storageClient = new StorageClient1(trackerServer, storageServer);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }

        return storageClient;
    }

    @Override
    public StorageServer getStorageServer(String filename){
        String storage_m00 = storage_other;//默认为图片目录

        if (FileCheck.checkImage(filename)){
            storage_m00 = storage_image;
        }else if (FileCheck.checkDoc(filename)){
            //文件
            storage_m00 = storage_doc;
        }else if (FileCheck.checkVideo(filename)){
            //视频音频
            storage_m00 = storage_video;
        }else if (FileCheck.checkCompress(filename)){
            //压缩文件
            storage_m00 = storage_compress;
        }else {
            storage_m00 = storage_other;
        }

        StorageServer storageServer = null;
        try {
            storageServer = new StorageServer(storage_server, Integer.parseInt(storage_port), Integer.parseInt(storage_m00));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return storageServer;
    }
}
